<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css"
          href="easyui/themes/bootstrap/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="easyui/demo/demo.css">
    <script type="text/javascript" src="easyui/jquery.min.js"></script>
    <script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>
    <%--<script type="text/javascript" src="myjs/merge_cell.js"></script>--%>
    <script type="text/javascript" src="myjs/path.js"></script>
    <script type="text/javascript" src="myjs/loader.js"></script>
    <style>
        .datagrid-footer {
            background-color: #f7c66c;
        }
    </style>
</head>
<body class="easyui-layout">
<div id="tb">
    <div>
    商品属性查询：年<input id="year"class="easyui-combobox" style="width: 120px;"  data-options="valueField:'year',textField:'year',panelHeight:'auto'">
    季<input id="season" class="easyui-combobox" style="width: 120px;" data-options="valueField:'season',textField:'season',panelHeight:'auto'">
    品牌<input id="brand" class="easyui-combobox" style="width: 120px;" data-options="valueField:'pinpai',textField:'pinpai',panelHeight:'auto'">
        <a href="#" class="easyui-linkbutton"
           data-options="iconCls:'icon-reload',plain:true" onclick="chongZhiss()">重置搜索</a>
    </div>

    <div>
        机构：<input id="tree" class="easyui-combotree" style="width: 160px;"/>
        日期查询从: <input id="qtime" type="text" class="easyui-datebox"
        data-options="editable:false" style="width: 120px"> 到: <input
            id="htime" type="text" class="easyui-datebox"
            data-options="editable:false" style="width: 120px">
        <label id='zt33'><input id='zt3' type='radio' onclick='load()' name='tj' value='2'><span>按尺码</span></label>
        <label id='zt22'><input id='zt2' type='radio' onclick='load()' name='tj' value='1'><span>按颜色</span></label>
        <label id='zt11'><input id='zt1' type='radio' onclick='load()' name='tj' value='0'
                                checked="checked"><span>按款号</span></label>
        <input id="kuanhao" class="easyui-searchbox"
               data-options="searcher:qq,prompt:'请输入款号关键词'">
        <%--<a id="btn" href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="load();">搜索</a>--%>
        <a iconCls="icon-save" class="easyui-linkbutton" type="button" onclick="tableToExcel()">导出Excel</a>
    </div>

</div>
<table class="easyui-datagrid" id="cxdm">

</table>

</body>
</html>
<script type="text/javascript">
    //导出表格
    function tableToExcel() {
        var shujuSize = $("#cxdm").datagrid("getRows").length;
        if (shujuSize == 0) {
            $.messager.show({
                title : '提示',
                msg : '没有数据不可导入,请查询以后在进行导出!',
                timeout : 1000,
                showType : 'slide'
            });
            return;
        }
        var kuanhao = $('#kuanhao').val();
        var qtime = $('#qtime').val(); // 需要查询的时间段(前
        var htime = $('#htime').val();
        var ordTJ =$('input:radio[name="tj"]:checked').val();
        var jgid = $("#tree").combotree("getValue");
        var cpYear = $("#year").combobox("getValue");
        var season = $("#season").combobox("getValue");
        var pinpai = $("#brand").combobox("getValue");
        window.location.href = path + "/report/downloadExcelMuban.do?selectText="
            + kuanhao + "&stime=" + qtime + "&etime=" +htime+"&jgid="+jgid+"&ordTJ="+ordTJ+"&cpYear="+cpYear+"&season="+season+"&pinpai="+pinpai;
    }

    function chongZhiss() {
        $('#kuanhao').searchbox('setValue', '');
        $("#year").combobox('setValue', '');
        $("#season").combobox('setValue', '');
        $("#brand").combobox('setValue', '');
        load();
    }

    function qq() {
        load();
    }

    $(document).ready(function () {
        $('#kuanhao').next('span').find('input').focus()
    });
</script>
<script>

    $(function () {

        $.ajax({
            url: path + "/production/selectallattr.do",
            type: 'post',
            dataType: "json",
            success: function (data) {
                console.log(data)
                $('#season').combobox("loadData", data.data.seasonx);
                $('#year').combobox("loadData", data.data.yearx);
                $('#brand').combobox("loadData", data.data.pinpaix);

            }


        })


        findTree();
        $("#tree").combotree({
            onSelect: function (node) {
                $('#cxdm').datagrid({
                    url: path + '/report/gtrukureport.do',
                    queryParams: {
                        jgid: node.id,
                        stime: $("#qtime").val(), // 需要查询的商品款号
                        etime: $("#htime").val(), // 需要查询的商品款号
                        selectText: $("#kuanhao").val(),
                        ordTJ: $('input:radio[name="tj"]:checked').val(),
                        cpYear: $("#year").combobox("getValue"),
                        season: $("#season").combobox("getValue"),
                        pinpai: $("#brand").combobox("getValue"),
                    }
                })
            },
            onLoadSuccess: function (node, data) {
                $("#tree").combotree('setValue', data[0].id);
                load();
            }
        });
    })

    function findTree() {
        //加载combotree
        $.ajax({
            url: path + '/jg/tree.do',
            dataType: 'json',
            success: function (data) {
                if (data.code != 400) {
                    $('#tree').combotree('loadData', data.rows);
                } else {
                    $.messager.alert('提示', data.msg);
                }
            }
        })

    }

    var date = new Date();//Tue Jul 16 01:07:00 CST 2013的时间对象
    var year = date.getFullYear();//年
    var month = date.getMonth() + 1;//月份（月份是从0~11，所以显示时要加1）
    var day = date.getDate();//日期
    var end = year + '-' + month + '-' + day;
    var start = year + '-' + month + '-' + 1;
    $("#qtime").val(start);
    $('#htime').val(end);


    $('#kuanhao').textbox({
        inputEvents: $.extend({}, $.fn.textbox.defaults.inputEvents, {
            keyup: function (event) {
                if (event.keyCode == 13) {
                    load();
                    $('#kuanhao').next('span').find('input').focus()
                }
            }
        })
    });

    //加载数据
    function load() {
        $('#cxdm').datagrid({
            url: path + '/report/gtrukureport.do',
            queryParams: {
                jgid: $("#tree").combotree("getValue"),
                stime: $("#qtime").val(), // 需要查询的商品款号
                etime: $("#htime").val(), // 需要查询的商品款号
                selectText: $("#kuanhao").val(),
                ordTJ: $('input:radio[name="tj"]:checked').val(),
                cpYear: $("#year").combobox("getValue"),
                season: $("#season").combobox("getValue"),
                pinpai: $("#brand").combobox("getValue"),
            }
        })
        var cpYear=$("#year").combobox("getValue");
        var  season= $("#season").combobox("getValue");
         var   pinpai= $("#brand").combobox("getValue");
        console.log(cpYear+"-"+season+"-"+pinpai)
    }

    $('#cxdm').datagrid(
        {
            width: 'auto',
            toolbar: '#tb',
            singleSelect: true,
            //loadMsg: '数据加载中请稍后……',
            pagination: true,
            pageSize: 20,//分页大小
            pageList: [20, 50, 60, 80],//每页的个数
            fit: true,//自动补全
            dataType: "json",
            showFooter: true,

            columns: [[{
                field: 'product',
                title: '商品款号',
                align: 'center',
                width: 200
            }, {
                field: 'productName',
                title: '名称',
                align: 'center',
                width: 150
            },{
                field: 'mprice',
                title: '铭牌价',
                align: 'center',
                width: 70,
                formatter: function (value, row, index) {
                    if (row.product == "总计：") {
                        return ''
                    }
                    return value;
                }
            },{
                field: 'cprice',
                title: '成本价',
                align: 'center',
                width: 70,
                formatter: function (value, row, index) {
                    if (row.product == "总计：") {
                        return ''
                    }
                    return value;
                }
            }, {
                field: 'cpYear',
                title: '年份',
                align: 'center',
                width: 70
            },{
                field: 'season',
                title: '季节',
                align: 'center',
                width: 80
            },{
                field: 'color',
                title: '颜色',
                align: 'center',
                width: 100
            }, {
                field: 'size',
                title: '尺码',
                align: 'center',
                width: 100
            },  {
                field: 'rkshu',
                title: '数量',
                align: 'right',
                width: 80
            },{
                field: 'zongji',
                title: '小计',
                align: 'right',
                width: 100
            }, {
                field: 'pinpai',
                title: '品牌',
                align: 'center',
                width: 150
            }, {//var startTimeStr=timdata.start_time.substr(0,19);
                field: 'rukushijian',
                title: '入库时间',
                align: 'center',
                width: 150,
                formatter: function (value, row, index) {
                    if(row.rukushijian!=null&&""!=row.rukushijian) {
                        var rksj = row.rukushijian.substr(0, 10);
                        return rksj;
                    }else {
                    return '';
                    }
                }
            }]],
            onLoadSuccess: function (data) {
                if ($('input:radio[name="tj"]:checked').val() == 0) {

                    $('#cxdm').datagrid('hideColumn', 'size');
                    $('#cxdm').datagrid('hideColumn', 'color');
                    $('#cxdm').datagrid('hideColumn', 'zongji');

                }
                if ($('input:radio[name="tj"]:checked').val() == 1) {

                    $('#cxdm').datagrid('hideColumn', 'size');
                    $('#cxdm').datagrid('showColumn', 'color');
                    $('#cxdm').datagrid('showColumn', 'zongji');

                }
                if ($('input:radio[name="tj"]:checked').val() == 2) {

                    $('#cxdm').datagrid('showColumn', 'size');
                    $('#cxdm').datagrid('showColumn', 'color');
                    $('#cxdm').datagrid('showColumn', 'zongji');
                }


                //给表格增加一行解决最后小计不算的问题
                $('#cxdm').datagrid('appendRow', {
                    product: '',
                    rukushijian: ''
                });
                var mark = 1;
                var colo = 1;
                var zongji = 0;
                var columns = $('#cxdm').datagrid("options").columns;
                var rows = $('#cxdm').datagrid("getRows");
                var count = 0;
                for (var i = 1; i < data.rows.length; i++) {
                    if (zongji == 0) {
                        zongji += data.rows[i - 1]['rkshu']
                    }
                    if (data.rows[i]['product'] == data.rows[i - 1]['product'] && data.rows[i]['rukushijian'].substr(0, 10) == data.rows[i - 1]['rukushijian'].substr(0, 10)) {
                        count++;
                        zongji += data.rows[i]['rkshu']
                    } else {
                        rows[i - 1 - count][columns[0][9].field] = zongji;
                        $('#cxdm').datagrid('refreshRow', i - 1 - count);
                        zongji = 0;
                        count = 0;
                    }
                }
                $('#cxdm').datagrid('deleteRow', data.rows.length - 1)

                for (var i = 1; i < data.rows.length; i++) {

                    if (data.rows[i]['product'] == data.rows[i - 1]['product'] && data.rows[i]['rukushijian'].substr(0, 10) == data.rows[i - 1]['rukushijian'].substr(0, 10)) {
                        mark += 1;
                        //console.log(data.rows[i])
                        $(this).datagrid('mergeCells', {
                            index: i + 1 - mark,
                            field: 'product',  //需要合并的列
                            rowspan: mark     //纵向合并的格数，如果想要横向合并，就使用colspan：mark
                        });

                        $(this).datagrid('mergeCells', {
                            index: i + 1 - mark,
                            field: 'productName',
                            rowspan: mark
                        });
                        $(this).datagrid('mergeCells', {
                            index: i + 1 - mark,
                            field: 'mprice',
                            rowspan: mark
                        });
                        $(this).datagrid('mergeCells', {
                            index: i + 1 - mark,
                            field: 'cprice',
                            rowspan: mark
                        });

                        $(this).datagrid('mergeCells', {
                            index: i + 1 - mark,
                            field: 'cpYear',
                            rowspan: mark
                        });  $(this).datagrid('mergeCells', {
                            index: i + 1 - mark,
                            field: 'season',
                            rowspan: mark
                        });
                        $(this).datagrid('mergeCells', {
                            index: i + 1 - mark,
                            field: 'rukushijian',
                            rowspan: mark
                        });

                        $(this).datagrid('mergeCells', {
                            index: i + 1 - mark,
                            field: 'zongji',
                            rowspan: mark

                        });
                        $(this).datagrid('mergeCells', {
                            index: i + 1 - mark,
                            field: 'pinpai',
                            rowspan: mark
                        });

                    } else {
                        mark = 1;     //一旦前后两行的值不一样了，那么需要合并的格子数mark就需要重新计算
                    }
                    if (data.rows[i]['color'] == data.rows[i - 1]['color'] && mark != 1) {
                        colo += 1;
                        $(this).datagrid('mergeCells', {
                            index: i + 1 - colo,
                            field: 'color',
                            rowspan: colo
                        });

                    } else {
                        colo = 1;     //一旦前后两行的值不一样了，那么需要合并的格子数colo就需要重新计算
                    }
                }
                $('#kuanhao').next('span').find('input').focus()
                disLoad();
            },

            onClickRow: function (rowIndex, rowData) {
                $(this).datagrid('unselectRow', rowIndex);
            },

        });
</script>

